package com.vincce.sport.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.entity.City;
import com.vincce.sport.entity.School;
import com.vincce.sport.entity.Team;
import com.vincce.sport.mapper.CityMapper;
import com.vincce.sport.mapper.SchoolMapper;
import com.vincce.sport.mapper.TeamMapper;

@Controller
@RequestMapping("/queryDataApi")
public class QueryDataApiController {

	@Autowired
	private CityMapper cityMapper;
	@Autowired
	private SchoolMapper schoolMapper;
	@Autowired
	private TeamMapper teamMapper;
	
	@RequestMapping("/getCity")
	@ResponseBody
	public String getCity(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> resMap = new HashMap<String, Object>();
		try {
			List<City> city = cityMapper.queryAllCity();
			resMap.put("suc", "true");
			resMap.put("msg", "请求成功");
			resMap.put("list", city);
		} catch (Exception e) {
			resMap.put("suc", "false");
			resMap.put("msg", "请求失败");
			e.printStackTrace();
		}
		
		return JSON.toJSONString(resMap);
	}
	
	@RequestMapping("/getSchoolByCityId")
	@ResponseBody
	public String getSchoolByCityId(HttpServletRequest request,String id){
		Map<String,Object> resMap = new HashMap<String, Object>();
		try {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("cityId", id);
			List<School> schoolList = schoolMapper.queryByCityId(map);
			resMap.put("suc", "true");
			resMap.put("msg", "请求成功");
			resMap.put("list", schoolList);
		} catch (Exception e) {
			resMap.put("suc", "false");
			resMap.put("msg", "请求失败");
			e.printStackTrace();
		}
		return JSON.toJSONString(resMap);
	}
	
	@RequestMapping("/getTeamBySchoolId")
	@ResponseBody
	public String getTeamBySchoolId(HttpServletRequest request,String id){
		Map<String,Object> resMap = new HashMap<String, Object>();
		try {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("schoolId", id);
			List<Team> teamList = teamMapper.queryTeamBySchoolId(map);
			resMap.put("suc", "true");
			resMap.put("msg", "请求成功");
			resMap.put("list", teamList);
		} catch (Exception e) {
			resMap.put("suc", "false");
			resMap.put("msg", "请求失败");
			e.printStackTrace();
		}
		return JSON.toJSONString(resMap);
	}
}
